home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / J A V A / Java Development Kit V1.2 / jdk12-win32(1).exe / data1.cab / demos / demo / jfc / SwingSet / ComboBoxPanel.java < prev    next >
Encoding:
Java Source  |  1998-12-01  |  10.0 KB  |  288 lines

  1. /*
  2.  * @(#)ComboBoxPanel.java    1.9 98/08/26
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. import javax.swing.*;
  16. import javax.swing.event.*;
  17. import javax.swing.text.*;
  18. import javax.swing.border.*;
  19. import javax.swing.plaf.basic.BasicComboBoxUI;
  20. import java.awt.*;
  21. import java.awt.event.*;
  22. import java.util.*;
  23. import javax.swing.plaf.*;
  24. import javax.swing.tree.*;
  25. import javax.swing.plaf.basic.BasicLookAndFeel;
  26. import javax.accessibility.*;
  27.  
  28. /**
  29.  * SwingSet panel for JComboBox
  30.  *
  31.  * @version 1.9 08/26/98
  32.  * @author Arnaud Weber
  33.  * @author Peter Korn (accessibility support)
  34.  */
  35. public class ComboBoxPanel extends JPanel {
  36.     // The Frame
  37.     SwingSet swing;
  38.     JComboBox  months;
  39.     JComboBox  days;
  40.     JComboBox  cb;
  41.     JComboBox  cb1;
  42.     JComboBox  custom;
  43.     JComboBox  treeComboBox;
  44.  
  45.     DefaultListModel model = new DefaultListModel();
  46.  
  47.     public ComboBoxPanel(SwingSet swing) {
  48.       JPanel tp;
  49.       this.swing = swing;
  50.       this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
  51.       setBorder(swing.emptyBorder5);
  52.       
  53.       this.add(Box.createRigidArea(new Dimension(1,50)));
  54.       JPanel panel = new JPanel(false);
  55.       panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
  56.       add(panel);
  57.       // Classic combo box
  58.       tp = new JPanel();
  59.       tp.setMaximumSize(new Dimension(Short.MAX_VALUE,100));
  60.       tp.setBorder(BorderFactory.createTitledBorder("Classic ComboBox"));
  61. //      tp.setTitle("Classic ComboBox");
  62.       tp.setLayout(new BoxLayout(tp,BoxLayout.X_AXIS));
  63.       tp.add(Box.createRigidArea(new Dimension(5,1)));
  64.       months = new JComboBox();
  65.       months.addItem("January");
  66.       months.addItem("February");
  67.       months.addItem("March");
  68.       months.addItem("April");
  69.       months.addItem("May");
  70.       months.addItem("June");
  71.       months.addItem("July");
  72.       months.addItem("August");
  73.       months.addItem("September");
  74.       months.addItem("October");
  75.       months.addItem("November");
  76.       months.addItem("December");
  77.       months.getAccessibleContext().setAccessibleName("Months");
  78.       months.getAccessibleContext().setAccessibleDescription("Choose a month of the year");
  79.       tp.add(months);
  80.       tp.add(Box.createRigidArea(new Dimension(5,1)));
  81.       days = new JComboBox();
  82.       days.addItem("Monday");
  83.       days.addItem("Tuesday");
  84.       days.addItem("Wednesday");
  85.       days.addItem("Thursday");
  86.       days.addItem("Friday");
  87.       days.addItem("Saturday");
  88.       days.addItem("Sunday");
  89.       days.getAccessibleContext().setAccessibleName("Days");
  90.       days.getAccessibleContext().setAccessibleDescription("Choose a day of the week");
  91.       tp.add(days);
  92.       tp.add(Box.createRigidArea(new Dimension(5,1)));
  93.       panel.add(tp);
  94.  
  95.       // Editable combo box
  96.  
  97.       add(panel);
  98.       tp = new JPanel();
  99.       tp.setMaximumSize(new Dimension(Short.MAX_VALUE,100));
  100.       tp.setBorder(BorderFactory.createTitledBorder("Editable ComboBox"));
  101.       tp.setLayout(new BoxLayout(tp,BoxLayout.X_AXIS));
  102.       tp.add(Box.createRigidArea(new Dimension(5,1)));
  103.       cb = new JComboBox();
  104.       cb.setEditable(true);
  105.       cb.addItem("0");
  106.       cb.addItem("10");
  107.       cb.addItem("20");
  108.       cb.addItem("30");
  109.       cb.addItem("40");
  110.       cb.addItem("50");
  111.       cb.addItem("60");
  112.       cb.addItem("70");
  113.       cb.addItem("80");
  114.       cb.addItem("90");
  115.       cb.addItem("100");
  116.       cb.addItem("More");
  117.       cb.getAccessibleContext().setAccessibleName("Numbers");
  118.       cb.getAccessibleContext().setAccessibleDescription("Demonstration editable ComboBox with numbers 0-100");
  119.       cb.setSelectedItem("50");
  120.       tp.add(cb);
  121.       tp.add(Box.createRigidArea(new Dimension(5,1)));
  122.  
  123.       cb1 = new JComboBox();
  124.       cb1.setEditable(true);
  125.       cb1.addItem("0");
  126.       cb1.addItem(".25");
  127.       cb1.addItem(".5");
  128.       cb1.addItem(".75");
  129.       cb1.addItem("1.0");
  130.       cb1.getAccessibleContext().setAccessibleName("Small numbers");
  131.       cb1.getAccessibleContext().setAccessibleDescription("Demonstration editable ComboBox with numbers 0-1");
  132.       cb1.setSelectedItem(".5");
  133.       tp.add(cb1);
  134.  
  135.       tp.add(Box.createRigidArea(new Dimension(5,1)));
  136.       panel.add(tp);
  137.  
  138.       // Custom combobox
  139.       panel = new JPanel(false);
  140.       panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
  141.       add(panel);
  142.       tp = new JPanel();
  143.       tp.setMaximumSize(new Dimension(Short.MAX_VALUE,200));
  144.       tp.setBorder(BorderFactory.createTitledBorder("Custom ComboBox"));
  145. //      tp.setTitle("Custom ComboBox");
  146.       tp.setLayout(new BoxLayout(tp,BoxLayout.X_AXIS));
  147.       tp.add(Box.createRigidArea(new Dimension(5,1)));
  148.       custom = new JComboBox(new CustomComboBoxModel());
  149.       custom.setRenderer(new TestCellRenderer(custom));
  150.       custom.setSelectedIndex(0);
  151.       custom.setMaximumRowCount(4);
  152.       custom.getAccessibleContext().setAccessibleName("Custom ComboBox");
  153.       custom.getAccessibleContext().setAccessibleDescription("Sample custom ComboBox with icons in them, one of which changes when selected");
  154.       tp.add(custom);
  155.       tp.add(Box.createRigidArea(new Dimension(5,1)));
  156.  
  157.       DefaultMutableTreeNode swingNode = new DefaultMutableTreeNode("Swing");
  158.       DefaultMutableTreeNode spec  = new DefaultMutableTreeNode("spec");
  159.       DefaultMutableTreeNode api   = new DefaultMutableTreeNode("api");
  160.  
  161.       swingNode.add(spec);
  162.       swingNode.add(api);
  163.  
  164.       api.add(new DefaultMutableTreeNode("JComponent"));
  165.       api.add(new DefaultMutableTreeNode("JTable"));
  166.       api.add(new DefaultMutableTreeNode("JTree"));
  167.       api.add(new DefaultMutableTreeNode("JComboBox"));
  168.       api.add(new DefaultMutableTreeNode("JTextComponent"));
  169.  
  170.       spec.add(new DefaultMutableTreeNode("JComponent"));
  171.       spec.add(new DefaultMutableTreeNode("JTable"));
  172.       spec.add(new DefaultMutableTreeNode("JTree"));
  173.       spec.add(new DefaultMutableTreeNode("JComboBox"));
  174.       spec.add(new DefaultMutableTreeNode("JTextComponent"));
  175.       
  176.       treeComboBox = new TreeCombo(new DefaultTreeModel(swingNode));
  177.       treeComboBox.getAccessibleContext().setAccessibleName("Swing specs and APIs");
  178.       treeComboBox.getAccessibleContext().setAccessibleDescription("Sample custom ComboBox with a tree heirarchy");
  179.       tp.add(treeComboBox);
  180.       tp.add(Box.createRigidArea(new Dimension(5,1)));
  181.       panel.add(tp);
  182.     }
  183.  
  184.     class CustomComboBoxModel extends AbstractListModel implements ComboBoxModel {
  185.       Object currentValue;
  186.       ImageIcon images[];
  187.       ImageIcon images_down[];      
  188.       Hashtable cache[];
  189.  
  190.       public CustomComboBoxModel() {
  191.     images = new ImageIcon[5];
  192.     images_down = new ImageIcon[5];
  193.     images[0] = SwingSet.sharedInstance().loadImageIcon("images/list/a1.gif","blue profile of robot");
  194.         images_down[0] = SwingSet.sharedInstance().loadImageIcon("images/list/a1d.gif","greyed out blue profile of robot");
  195.     images[1] = SwingSet.sharedInstance().loadImageIcon("images/list/a2.gif","pinkish profile of robot");
  196.         images_down[1] = SwingSet.sharedInstance().loadImageIcon("images/list/a2d.gif","greyed out pinkish profile of robot");
  197.     images[2] = SwingSet.sharedInstance().loadImageIcon("images/list/a3.gif","yellow profile of robot");
  198.         images_down[2] = SwingSet.sharedInstance().loadImageIcon("images/list/a3d.gif","greyed out yellow profile of robot");
  199.     images[3] = SwingSet.sharedInstance().loadImageIcon("images/list/a4.gif","green profile of robot");
  200.         images_down[3] = SwingSet.sharedInstance().loadImageIcon("images/list/a4d.gif","greyed out green profile of robot");
  201.     images[4] = SwingSet.sharedInstance().loadImageIcon("images/list/a5.gif","profile of robot");
  202.         images_down[4] = SwingSet.sharedInstance().loadImageIcon("images/list/a5d.gif","greyed out profile of robot");
  203.     cache = new Hashtable[getSize()];
  204.       }
  205.  
  206.       public void setSelectedItem(Object anObject) {
  207.     currentValue = anObject;
  208.     fireContentsChanged(this,-1,-1);
  209.       }
  210.       
  211.       public Object getSelectedItem() {
  212.     return currentValue;
  213.       }
  214.  
  215.       public int getSize() {
  216.     return 25;
  217.       }
  218.  
  219.       public Object getElementAt(int index) {
  220.     if(cache[index] != null)
  221.       return cache[index];
  222.     else {
  223.       Hashtable result = new Hashtable();
  224.         if(index != 24) {
  225.           result.put("title","Hello I'm the choice " + index);
  226.           result.put("image",images[index % 5]);
  227.           result.put("Himage",images_down[index % 5]);
  228.         } else {
  229.           result.put("title","Hello I'm Duke");
  230.           result.put("image",swing.dukeSnooze);
  231.           result.put("Himage",swing.dukeWave);
  232.         }
  233.       cache[index] = result;
  234.       return result;
  235.     }
  236.       }
  237.     }
  238.  
  239.     class TestCellRenderer extends JLabel implements ListCellRenderer   {
  240.     JComboBox combobox;
  241.  
  242.  
  243.       public TestCellRenderer(JComboBox x) {
  244.         this.combobox = x;
  245.         setOpaque(true);
  246.       }
  247.  
  248.       public Component getListCellRendererComponent(
  249.           JList listbox, 
  250.           Object value, 
  251.           int index, 
  252.           boolean isSelected, 
  253.           boolean cellHasFocus) 
  254.       {
  255.     Hashtable h = (Hashtable) value;
  256.         if(UIManager.getLookAndFeel().getName().equals("CDE/Motif")) {
  257.             if(index == -1 )
  258.                 setOpaque(false);
  259.             else
  260.                 setOpaque(true);
  261.         } else 
  262.             setOpaque(true);
  263.  
  264.         if(value == null) {
  265.       setText("");
  266.       setIcon(null);
  267.     } else if(isSelected) {
  268.         setBackground(UIManager.getColor("ComboBox.selectionBackground"));
  269.         setForeground(UIManager.getColor("ComboBox.selectionForeground"));
  270.             setIcon((ImageIcon)h.get("Himage"));
  271.             setText((String)h.get("title"));
  272.     } else {
  273.             setIcon((ImageIcon)h.get("image"));
  274.             setText((String)h.get("title"));
  275.         setBackground(UIManager.getColor("ComboBox.background"));
  276.         setForeground(UIManager.getColor("ComboBox.foreground"));
  277.     }
  278.     return this;
  279.       }
  280.     }
  281. }
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.